package com.allwinter.yibangsafe.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.allwinter.yibangsafe.domain.MyService;
import com.allwinter.yibangsafe.mapper.myServiceMapper;
import com.allwinter.yibangsafe.service.myServiceService;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyServiceServiceImpl implements myServiceService {

    @Autowired(required = false)
    private myServiceMapper myServiceMapper;

    @Override
    public JSONObject getAllService() {
        JSONObject res = new JSONObject();
        try {
            List<MyService> myServices = myServiceMapper.selectList(null);
            res.put("code",1);
            res.put("ServiceList",myServices);
            res.put("msg","getAllService success");
        } catch (Exception e) {
            res.put("code",0);
            res.put("msg",e.toString());
        }
        return res;
    }

    @Override
    public JSONObject getByServiceId(Long serviceid) {
        JSONObject res = new JSONObject();
        try {
            MyService myService = myServiceMapper.selectById(serviceid);
            res.put("code",1);
            res.put("Service",myService);
            res.put("msg","getServiceById success");
        } catch (Exception e) {
            res.put("code", 0);
            res.put("msg", e.toString());
        }
        return res;
    }

    @Override
    public JSONObject getByuserId(Long userid) {
        JSONObject res = new JSONObject();
        try {
            LambdaQueryWrapper<MyService> myServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            myServiceLambdaQueryWrapper.eq(MyService::getUserid,userid);
            MyService byOneParam = myServiceMapper.getByOneParam(myServiceLambdaQueryWrapper);
            res.put("code",1);
            res.put("Service",byOneParam);
            res.put("msg","getServiceByUserId success");
        } catch (Exception e) {
            res.put("code", 0);
            res.put("msg", e.toString());
        }
        return res;
    }

    @Override
    public JSONObject selectByType(String type) {
        JSONObject res = new JSONObject();
        try {
            LambdaQueryWrapper<MyService> myServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            myServiceLambdaQueryWrapper.eq(MyService::getType,type);
            MyService byOneParam = myServiceMapper.getByOneParam(myServiceLambdaQueryWrapper);
            res.put("code",1);
            res.put("Service",byOneParam);
            res.put("msg","getServiceByType success");
        } catch (Exception e) {
            res.put("code", 0);
            res.put("msg", e.toString());
        }
        return res;
    }

    @Override
    public JSONObject selectByPage(int nowPage, int pageSize) {
        JSONObject res = new JSONObject();
        try {
            Page<MyService> page = new Page<>(nowPage,pageSize);
            myServiceMapper.selectPage(page,null);

            List<MyService> myServices = page.getRecords();

            res.put("code",1);
            res.put("Service",myServices);
            res.put("pages",page.getPages());
            res.put("msg","getServiceByType success");
        } catch (Exception e) {
            res.put("code", 0);
            res.put("msg", e.toString());
        }
        return res;
    }
}
